
package com.sun.showcase.biz.mall.service.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.showcase.biz.basic.service.UserInfoService;
import com.sun.showcase.biz.mall.dao.ShopCashApplyDao;
import com.sun.showcase.biz.mall.service.PlatformConfigService;
import com.sun.showcase.biz.mall.service.ShopBillService;
import com.sun.showcase.biz.mall.service.ShopCashApplyService;
import com.sun.showcase.client.domain.basic.UserInfo;
import com.sun.showcase.client.domain.mall.PlatformConfig;
import com.sun.showcase.client.domain.mall.ShopCashApply;
import com.sun.showcase.client.query.mall.ShopBillQuery;
import com.sun.showcase.client.query.mall.ShopCashApplyQuery;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Pager;
import com.sun.showcase.utils.ArithUtil;
import com.sun.showcase.utils.LoginContextHolder;
import com.sun.showcase.utils.StringConstant;
import com.sun.showcase.utils.UuidUtil;
@Service("shopCashApplyService")
@Transactional
public class ShopCashApplyServiceImpl implements ShopCashApplyService{
	@Autowired
	private ShopCashApplyDao shopCashApplyDao;
	@Autowired
	private ShopBillService shopBillService;
	@Autowired
	private UserInfoService userInfoService;
	@Autowired
	private PlatformConfigService platformConfigService;
	public void setShopCashApplyDao(ShopCashApplyDao dao) {
		this.shopCashApplyDao = dao;
	}

	public DataGrid datagrid(ShopCashApplyQuery shopCashApplyQuery) {
		DataGrid j = new DataGrid();
		Pager<ShopCashApply> pager  = find(shopCashApplyQuery);
		j.setRows(getQuerysFromEntitys(pager.getRecords()));
		j.setTotal(pager.getTotalRecords());
		return j;
	}

	private List<ShopCashApplyQuery> getQuerysFromEntitys(List<ShopCashApply> shopCashApplys) {
		List<ShopCashApplyQuery> shopCashApplyQuerys = new ArrayList<ShopCashApplyQuery>();
		if (shopCashApplys != null && shopCashApplys.size() > 0) {
			for (ShopCashApply tb : shopCashApplys) {
				ShopCashApplyQuery b = new ShopCashApplyQuery();
				BeanUtils.copyProperties(tb, b);
				shopCashApplyQuerys.add(b);
			}
		}
		return shopCashApplyQuerys;
	}

	private Pager<ShopCashApply> find(ShopCashApplyQuery shopCashApplyQuery) {
		Page<ShopCashApply> page = PageHelper.startPage(shopCashApplyQuery.getPage().intValue(), shopCashApplyQuery.getRows().intValue());
		List<ShopCashApply> list = shopCashApplyDao.findList(shopCashApplyQuery);
		Pager<ShopCashApply> pager = new Pager<ShopCashApply>();
		pager.setRecords(list);
		pager.setTotalRecords(page.getTotal());
		return  pager;
		
	}
	


	public ShopCashApply add(ShopCashApplyQuery shopCashApplyQuery) {
		ShopCashApply t = new ShopCashApply();
		BeanUtils.copyProperties(shopCashApplyQuery, t);
		t.setCreateDate(new Date());
	    t.setId(UuidUtil.get32UUID());
	    t.setStatus(StringConstant.ZERO);
	    t.setOperaterId(LoginContextHolder.get().getUserId());
	    t.setOperaterName(LoginContextHolder.get().getUserName());
		shopCashApplyDao.save(t);
		shopCashApplyQuery.setId(t.getId());
		//账单
		PlatformConfig conf = platformConfigService.get();
		if(conf == null) {
			conf = new PlatformConfig();
			conf.setShopCashFee(0D);
		}
		ShopBillQuery ad = new ShopBillQuery();
		ad.setBussinessId(t.getId());
		ad.setAmount(t.getAmount());
		ad.setServiceFee(ArithUtil.sub(t.getAmount(), conf.getShopCashFee()));
		ad.setActualAmount(ArithUtil.sub(ad.getAmount(), ad.getServiceFee()));
		ad.setType("1");
		ad.setStatus("0");
		ad.setUserId(t.getUserId());
		ad.setUserName(t.getUserName());
		ad.setRemark("商家提现");
		shopBillService.add(ad);
		//扣除余额
		userInfoService.updateBalance(t.getUserId(), t.getAmount()*-1);
		
		UserInfo ui = userInfoService.get(t.getUserId());
		
		t.setAmount(ui.getBalance());
		return t;
		
	}

	public void update(ShopCashApplyQuery shopCashApplyQuery) {
		ShopCashApply t = shopCashApplyDao.getById(shopCashApplyQuery.getId());
		
	    if(t != null) {
	    	if("0".equals(t.getStatus()) && "1".equals(shopCashApplyQuery.getStatus())) {
	    		//审核通过，扣除余额
	    		
	    	}else if("1".equals(t.getStatus()) && "3".equals(shopCashApplyQuery.getStatus())) {
	    		//确认转账，修改账单状态
	    		shopBillService.updateStatus(t.getId(), "1");
	    	}else if("0".equals(t.getStatus()) && "2".equals(shopCashApplyQuery.getStatus())) {
	    		//不通过，返还余额
	    		userInfoService.updateBalance(t.getUserId(), t.getAmount());
	    		shopBillService.updateStatus(t.getId(), "2");
	    	}
	    	BeanUtils.copyProperties(shopCashApplyQuery, t);
	    	t.setOperaterId(LoginContextHolder.get().getUserId());
	  	    t.setOperaterName(LoginContextHolder.get().getUserName());
	    	shopCashApplyDao.update(t);
		}
	}
	
	/**
	 * 根据条件更新部分字段   新建一个query封装需要更新的字段
	 * searchMap封装更新条件
	 * */
	public void updatePart(ShopCashApplyQuery shopCashApplyQuery) {
	    shopCashApplyDao.updatePart(shopCashApplyQuery);
	}

	public void delete(java.lang.String[] ids) {
		shopCashApplyDao.deleteAc(ids);
	}
	
	//逻辑删除 更新状态位
	public void deletePt(java.lang.String[] ids) {
		shopCashApplyDao.deletePt(ids);
	}

	public ShopCashApply get(ShopCashApplyQuery shopCashApplyQuery) {
		return shopCashApplyDao.getById(shopCashApplyQuery.getId());
	}

	public ShopCashApply get(String id) {
		return shopCashApplyDao.getById(id);
	}

	
	public List<ShopCashApplyQuery> listAll(ShopCashApplyQuery shopCashApplyQuery) {
	    List<ShopCashApply> list = shopCashApplyDao.findList(shopCashApplyQuery);
		List<ShopCashApplyQuery> listQuery =getQuerysFromEntitys(list) ;
		return listQuery;
	}
	
	
}
